package stu.day25_day32.day25.day1;

import stu.day25_day32.day25.all.TreeNode;

import java.util.*;


public class LeetCode637 {
    public List<Double> averageOfLevels(TreeNode root) {


        List<Double> list = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();

        if(root==null){
            return list;
        }

        queue.offer(root);

        while (!queue.isEmpty()){
            int size = queue.size();
            double num = 0;
            double len = 0;
            for(int i=0;i<size;i++){
                TreeNode t = queue.poll();
                if(t.left!=null){
                    queue.offer(t.left);
                }
                if(t.right!=null){
                    queue.offer(t.right);
                }
                num += t.val;
                len += 1;
            }

            list.add(num/len);

        }

        return list;

    }

}
